$(function () {
    let layer = layui.layer;
    let form = layui.form;
    let addIndex;
    let editIndex;
    // 获取类别
    function getCategory() {
        $.ajax({
            method: 'get',
            url: '/my/article/cates',
            success: function (res) {
                console.log(res);
                if (res.status !== 0) {
                    return layer.msg('类别获取失败')
                }
                let txt = template('tpl-get', res);
                $("tbody").html(txt)
            }
        })
    }
    // 调用获取类别函数
    getCategory();

    // 点击添加类别按钮
    $('#addCateBtn').on('click', function () {
        addIndex = layer.open({
            title: '添加文章分类'
            , content: $('#tpl-add').html()
            , type: 1
            , area: ['500px', '250px']
        });

    })

    // 点击确认添加类别按钮
    $('body').on('submit', '#addForm', function (e) {
        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize(),
            success: function (res) {
                console.log(res);
                if (res.status !== 0) {
                    return layer.msg('添加失败')
                }
                layer.msg('添加成功')

                layer.close(addIndex);

                getCategory()
            }
        })
    })

    // 点击编辑按钮
    $('tbody').on('click', '.editBtn', function () {

        let id = $(this).attr('data-id')
        console.log(id);
        editIndex = layer.open({
            title: '修改文章分类'
            , content: $('#tpl-edit').html()
            , type: 1
            , area: ['500px', '250px']
        });


        $.ajax({
            method: 'GET',
            url: '/my/article/cates/' + id,
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg('获取信息失败')
                }
                form.val('editForm', res.data)
            }
        })
    })

    // 点击确认修改按钮
    $('body').on('submit', '#editForm', function (e) {
        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize(),
            success: function (res) {
                console.log(res);
                if (res.status !== 0) {
                    return layer.msg('编辑失败')
                }
                layer.msg('编辑成功')

                layer.close(editIndex);

                getCategory()
            }
        })
    })

    // 点击删除按钮
    $('tbody').on('click', '.deleteBtn', function () {
        console.log($(this)[0]);
        let id = $(this).attr('data-id')
        console.log(id);

        layer.confirm('是否删除？', { icon: 3, title: '提示' }, function (index) {

            $.ajax({
                method: 'GET',
                url: '/my/article/deletecate/' + id,
                success: function (res) {
                    console.log(res);
                    if (res.status !== 0) {
                        return layer.msg('删除失败')
                    }
                    layer.msg('删除成功')



                    getCategory()
                }
            })

            layer.close(index);
        });
    })
})